package com.gewu.teday.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class TestRandom {

    private static int MIN;
    private static float MAX;
    private static float RANGE;

    public static List<String> splitV(float total, int count, int min) {
        List<String> list = new ArrayList<String>();
        MIN = min;
        MAX = total / 30 + 30;
        RANGE = MAX - min;
        if (MIN >= total/30-10) {
            return list;
        }
        float changed = total - MIN * count;
        split(list, changed, count);
        return list;
    }


    public static void split(List<String> list, float changed, int count) {
        float val = 0;
        float maxChanged = 0;

        if (count == 1) {
            val = changed + MIN;
            double score = new BigDecimal(val).setScale(2, RoundingMode.HALF_UP).doubleValue();
            DecimalFormat df = new DecimalFormat("#0.00");
            list.add(df.format(score));
            return;
        }
        maxChanged = changed - RANGE * (count - 1);

        float base = 0;
        float curChanged = 0;
        //如果maxchanage >0 说明后续的剩余比较多
        if (maxChanged > 0) {
            base = maxChanged;
            maxChanged = RANGE - base;
        }
        // 判断当前变动 大小 小于0  一般说明后续剩余不是特别多
        if (maxChanged < 0) {
            if (changed > RANGE) {
                maxChanged = RANGE;
            } else {
                maxChanged = changed;
            }
        }

        curChanged = (float) (Math.random() * (maxChanged + 1)) + base;
        val = curChanged + MIN;
        count--;

        double v;

        if (MAX*30<10000){
            if (MAX - 20 < val && val < MAX  + 20) {
                v = (double) new Random().nextInt(44);
            } else {
                v = (double) new Random().nextInt(9);
            }
        }else {
            if (MAX - 80 < val && val < MAX  + 80) {
                v = (double) new Random().nextInt(60);

            } else {
                v = (double) new Random().nextInt(9);
            }
        }
        if (count%2==0){
            val-=v;
        }else {
            val+=v;
        }

        //随机结果 保留两位小数  最后一位小数为0不显示时 自动补0
        double score = new BigDecimal(val+new Random().nextDouble()).setScale(2, RoundingMode.HALF_UP).doubleValue();
        DecimalFormat df = new DecimalFormat("#0.00");
        list.add(df.format(score));
        split(list, changed - curChanged, count);
    }
}